package GUI;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

import Monitoring.Settings;
import Monitoring.StatusReader;
import Structs.Key;

public class MainInterface extends JFrame{

	/**
	 * 
	 */
	private static final long serialVersionUID = -6896327098030597008L;
	private JLabel programStatus; 
	private JButton startStopButton;
	private StatusReader status;
	private Settings settings;
	
	public MainInterface(){
		this.settings = new Settings();
		System.out.println("Is settings null? " + settings == null);
		loadSettings();
		System.out.println("Settings loaded");
		this.status = new StatusReader(settings);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setup();
		this.pack();
		this.setVisible(true);
	}
	
	public void loadSettings(){
		File f = new File("OBSLolSceneSwapper.ini");
		if(f.exists()){
			BufferedReader br;
			try {
				br = new BufferedReader(new FileReader("OBSLolSceneSwapper.ini"));
				
				String line = br.readLine();
				settings.setFileDirectory(line.substring(6, line.length()));
				 line = (line = br.readLine()).substring(15, line.length());
				settings.defaultSceneKey = new Key(line.split(" ")[0],Integer.parseInt((line.split(" ")[1])));
				line = (line = br.readLine()).substring(20, line.length());
				settings.charSelectionKey = new Key(line.split(" ")[0],Integer.parseInt((line.split(" ")[1])));
				line = (line = br.readLine()).substring(14, line.length());
				settings.ingameKey = new Key(line.split(" ")[0],Integer.parseInt((line.split(" ")[1])));
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}	
		}
	}
	
	public void setup(){
		//Status 
		programStatus = new JLabel("Status: Stopped");
		this.add(programStatus, BorderLayout.NORTH);
		//Start/Stop Button
		startStopButton = new JButton("Start");
		
		startStopButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent arg0) {
				// TODO Auto-generated method stub
					if(startStopButton.getText().toString().equals("Start")){
						if(status.startProgram()){
							programStatus.setText("Status: Running");
							startStopButton.setText("Stop");							
						}
					}else{
						status.stopProgram();
						programStatus.setText("Status: Stopped");
						startStopButton.setText("Start");
					}
				
			}
			
		});
		this.add(startStopButton, BorderLayout.CENTER);
		
		JButton settingsButton = new JButton("Settings");
		this.add(settingsButton, BorderLayout.SOUTH);
		settingsButton.addActionListener(new ActionListener(){

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				new SettingsInterface(settings);
			}
			
		});
	}
	
	public static void main(String[] args){
		new MainInterface();
	}
}
